package com.zl.Sqlite.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.zl.Sqlite.DBheloper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * User: c3t
 * Date: 14-5-2
 * Time: 上午4:36
 */
public class SQLiteTemplate {
    private DBheloper dBheloper;

    public SQLiteTemplate(Context context) {
        dBheloper = DBheloper.getInstance(context);
    }

    private void executeRead(MyCallback myCallback) {
        SQLiteDatabase db = dBheloper.getReadableDatabase();
        myCallback.doTemplate(db);
        db.close();
    }

    private void executeWrite(MyCallback myCallback) {
        SQLiteDatabase db = dBheloper.getWritableDatabase();
        myCallback.doTemplate(db);
        db.close();
    }

    /**
     * 添加
     *
     * @param sql
     * @param objs
     */
    public void add(final String sql, final Object[] objs) {
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                db.execSQL(sql, objs);
            }
        });
    }

    /**
     * 判断用户是否存在
     *
     * @param sql  查询的sql语句
     * @param objs 参数数组
     */
    public ResulstObject find(final String sql, final String[] objs) {
        final ResulstObject resulstObject = new ResulstObject();
        executeRead(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                Cursor cursor = db.rawQuery(sql, objs);
                boolean exist = cursor.moveToNext();
                cursor.close();
                resulstObject.setExist(exist);
            }
        });
        return resulstObject;
    }

    /**
     * 更新
     *
     * @param sql
     * @param objs
     */
    public void update(final String sql, final Object[] objs) {
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                db.execSQL(sql, objs);
            }
        });
    }

    /**
     * 删除
     *
     * @param sql
     * @param objs
     */
    public void delete(final String sql, final Object[] objs) {
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                db.execSQL(sql, objs);
            }
        });
    }

    /**
     * 得到所有用户
     * @param sql
     * @return
     */
    public ResulstObject find(final String sql) {
        final ResulstObject resulstObject = new ResulstObject();
        executeRead(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                ArrayList<Object> rs = new ArrayList<>();
                Cursor cursor = db.rawQuery(sql, null);
                while (cursor.moveToNext()) {
                    Map<Object,Object> map = new HashMap<>();
                    int id = cursor.getInt(cursor.getColumnIndex("id"));
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    map.put("id",id);
                    map.put("name",name);
                    rs.add(map);
                }
                resulstObject.setResult(rs);
            }
        });
        return resulstObject;
    }
}
